$(function(){
	page.bind();
	page.getRewardAndPunishInfo();
});
var page = {
	//获取奖惩制度信息
	getRewardAndPunishInfo:function(){
		ajaxUtil.ajaxRuquest(
				"dingTalk/getRewardAndPunishInfo",
				{},
				function(data){
					console.log(data);
					var length1 = $("#late-rightDiv .minute").length;
					var length2 = $("#late-rightDiv .yuan").length;
					var length3 = $("#absent-rightDiv input").length;
					var length4 = $("#leave-rightDiv input").length;
					var length5 = $("#overtime-rightDiv input").length;
					if(length1<data.length){
						var tt = length1;
						for(;tt<data.length;tt++){
							$("#late-rightDiv").append("<div><label >小于</label>&nbsp; <input type='text' class='input-text minute' value='"+data[tt].clocking+"'/>（分钟）&nbsp;&nbsp; <label>应扣：</label>&nbsp;<input type='text' class='input-text yuan' value='"+data[tt].clockingCharge+"'/>&nbsp;<span>元</span></br></br></div>");
						}
					}
					for(var i=0,leng=data.length;i<leng;i++){
						if(i<length1){
							$("#late-rightDiv .minute:eq("+i+")").val(data[i].clocking);
							$("#late-rightDiv .yuan:eq("+i+")").val(data[i].clockingCharge);
						}
						if(i<length3){
							$("#absent-rightDiv input:eq("+i+")").val(data[i].absent);
						}
						if(i<length4){
							$("#leave-rightDiv input:eq("+i+")").val(data[i].leave);
						}
						if(i<length5){
							$("#overtime-rightDiv input:eq("+i+")").val(data[i].overtime);
						}
					}
				}
		);
	},
    bind: function () {
    	$("#plus").bind("click",function(){
    		if($("#late-rightDiv div").length<6){
    			$("#late-rightDiv").append("<div><label >小于</label>&nbsp; <input  type='text' class='input-text minute'/>（分钟）&nbsp;&nbsp; <label>应扣：</label>&nbsp;<input type='text' class='input-text yuan'/>&nbsp;<span>元</span></br></br></div>");
    		}
    	});
    	$("#subtract").bind("click",function(){
    		if($("#late-rightDiv div").length>3){
    			$("#late-rightDiv div:last").remove();
    		}
    	});
        $(".input-text").blur(function(){
            if($(this).val()!="" && !/^\d+(\.{0,1}\d+){0,1}$/.test($(this).val())){
                layer.msg("请输入非负数");
                $(this).val("");
            }
        });
        $(".minute").bind("blur", function () {
            if($(this).val()!="" && !/^\+?[1-9][0-9]*$/.test($(this).val())){
                layer.msg("请输入非0的正整数");
                $(this).val("");
            }
            //还应校验值的大小，后面的值大于前面的值
        });
        $("#save").bind("click",function(){
            var length = $("#late-rightDiv .minute").length;
            console.log(length);
            var array1 = new Array();
            var array2 = new Array();
            var array3 = new Array();
            var array4 = new Array();
            var array5 = new Array();
            //一列一个array
            //迟到早退分钟数
            $("#late-rightDiv .minute").each(function(){
            	array1.push($(this).val());
            });
            //迟到早退扣款数
            $("#late-rightDiv .yuan").each(function(){
                array2.push($(this).val())
            });
            //旷工扣款比例
            $("#absent-rightDiv input").each(function(){
                array3.push($(this).val())
            });
            //请假扣款比例
            $("#leave-rightDiv input").each(function(){
                array4.push($(this).val())
            });
            //加班工资比例
            $("#overtime-rightDiv input").each(function(){
                array5.push($(this).val())
            });
            console.log(array1);
            console.log(array2);
            console.log(array3);
            console.log(array4);
            console.log(array5);
            //奖惩设置  发起请求，传输array，
            $.ajax({
    			url:'dingTalk/rewardAndPunishSet',//insertAttendanceRewardAndPunish
    			type:'POST',
    			data:{'array1':array1,
                    'array2':array2,
                    'array3':array3,
                    'array4':array4,
                    'array5':array5},
    			dataType:'json',
    			traditional:true,
    			success:function(data,status,xhr){
    				if(xhr.getResponseHeader("permissionStatus")=='noPermission'){
    					layAlert("没有权限");
    					return false;
    				}
    				if(data.result){
    					data=data.data;
    					layer.msg(data,{"icon":1});
    				}else{
    					layAlert(data.info);
    				}
    			}
    		});
        });
    }
};